// mobile bind
// ============

define(['zepto', 'utils', 'JST', 'page/jst/course-action-group', 'sui', 'submitForm',], function($, utils, JST, actionGroup){
  return {
    init: function(data) {
      this.roundId = data.roundId;
      this.courseScheduleId = data.courseScheduleId;
      
      // TODO add validate here
      this.onDeteleMovement();
      this.onClickAllSelect();
      this.submitDelete();
      this.submitEdit();
      
      actionGroup.init(window.__CTX__, data)
    },
    
    onDeteleMovement: function() {
        $(document).on("click", ".to-delete-button", function() {
          if(!$(this).data("delete")) {
            $(this).find("span").html("取消")
            $(".add-action-box").hide();
            $(".delete-movement").show();
            $(".delete-box").show();
            $(".delete-button").parent().hide();
            $(this).data("delete", true)
            $(".edit").parent().hide();
            $("input[name='_method']").val("DELETE");
          } else {
            $(this).find("span").html("删除")
            $(".add-action-box").show();
            $(".delete-movement").hide();
            $(".delete-box").hide();
            $(this).data("delete", false);
            $(".edit").parent().show();
            $("input[name='_method']").val("PATCH");
          }
        });
        
        $(document).on("change", "input[type='checkbox']", function() {
          var num = $("input[name='coursewareId']:checked").length;
          
          if(num > 0) {
            $(".delete-button").parent().show();
          } else {
            $(".delete-button").parent().hide();
          }
        });
    },
    
    onClickAllSelect: function() {
        $(document).on("click", '.all-select', function() {
          if($(this).prop("checked")) {
            $(".delete-movement input[type='checkbox']").prop("checked", true)
          } else {
            $(".delete-movement input[type='checkbox']").prop("checked", false)
          }
        })
    },
    
    submitDelete: function() {
      var that = this;
      $(document).on("click", ".delete-button", function() {
        $.showIndicator();
        $.ajax({
          url: $("form[name='delete_form']").attr("action"),
          type: 'POST',
          data: $("form[name='delete_form']").serializeArray(),
          success: function(res) {
            $.hideIndicator()
            var res = JSON.parse(res)
            if(res.code == "0") {
              window.location.reload();
            } else {
              $.toast(res.message);
            }
          },
          fail: function() {
            $.hideIndicator()
            $.toast("请求失败");
          }
        });
      });
    },
    
    submitEdit: function() {
      var that = this;
      $(document).on("click", ".edit", function() {
          var flag = true;
          $("input[name$='groupAmount']").each(function(){
              if(flag && !$(this).val()) {
                $.toast('组数不能为空');
                flag = false;
              }
          });
          
          if(!flag) {
            return false;
          }
          
          $("input[name$='movementNumber']").each(function(){
              if(flag && !$(this).val()) {
                $.toast('每组次数不能为空');
                flag = false;
              }
          });
          
          if(!flag) {
            return false;
          }
          $.showPreloader();
          $.ajax({
            url: window.__CTX__.root + '/rounds/'+that.roundId+'/course-coursewares/'+that.courseScheduleId+'/coursewares',
            type: 'POST',
            data: $("form[name='delete_form']").serializeArray(),
            success: function(res) {
              $.hidePreloader();
              
              var res = JSON.parse(res)
              if(res.code == "0") {
                $.toast(res.message);
                setTimeout(function(e){
                    window.location.reload();
                }, 1000)
              } else {
                $.toast(res.message);
              }
            },
            fail: function() {
              $.hidePreloader();
              $.toast("请求失败");
            }
          });
        });
    }
  }
})

